package com.pace.gonu.util;

import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class JSONMapper {
	/**
	 * 내부 로직에서 객체를 json 요청이 
	 * @param obj
	 * @return
	 */
	static public String objectToApiJsonStringWithOutMessage(Object obj) {
		ObjectMapper objectMapper = new ObjectMapper();
		StringWriter stringWriter = new StringWriter();
		JsonFactory jsonFactory = objectMapper.getJsonFactory();
		JsonGenerator jsonGenerator = null;
		try {
			jsonGenerator = jsonFactory.createJsonGenerator(stringWriter);
			jsonGenerator.writeObject(obj);
		} catch (IOException e) {
			throw new IllegalStateException(e);
		}
		return stringWriter.toString();
	}

	static public Map<String, Object> stringToJsonMap(String jsonString) throws JsonParseException, JsonMappingException, UnsupportedEncodingException, IOException {
		ObjectMapper objectMapper = new ObjectMapper();
		Map<String, Object> output = objectMapper.readValue(jsonString, new TypeReference<Map<String, Object>>() {
		});
		return output;
		

	}
}
